﻿using System;
using System.IO;
using System.Linq;
using Biz.Buffer;
using Biz.EF.Platform;
using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    /// <summary>
    /// 处理2011
    /// </summary>
    public static class UplineFromPksc
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(UplineFromPksc));

        public static void Process2011(string content)
        {
            try
            {
                Log.Debug(content);
                var mn = Parser.GetStringByKey("MN", content);
                if (String.IsNullOrWhiteSpace(mn)) return;
                var mapping = CommonInformation.GetTableMappingByPkscMn(mn);
                if (mapping == null) return;

                using (var ctx = new PlatformContext())
                {
                    var realDataMain = new RealDataMain();
                    realDataMain.企业基本信息ID = mapping.enterprise_id;
                    realDataMain.设备信息ID = mapping.port_id;
                    realDataMain.污染物类别ID = 2;

                    float? masterFactor = null;

                    foreach (var key in MasterFactors.Factors)
                    {
                        masterFactor = Parser.GetFloatByKey(key + "-Rtd", content);
                        if (masterFactor != null)
                        {
                            break;
                        }
                    }

                    realDataMain.RTD = masterFactor??0;
                    realDataMain.监测时间 = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                    ctx.RealDataMains.Add(realDataMain);
                    ctx.SaveChanges();

                    var factors = Parser.GetPollutantCodes(content);
                    foreach (var key in MasterFactors.Factors)
                    {
                        factors.Remove(key);
                    }

                    foreach (var factor in factors)
                    {
                        var factorCode = CommonInformation.GetMonitorFactorCodes(factor);
                        if (factorCode == null) continue;
                        var realDataDetail = new RealDataDetail();
                        realDataDetail.主实时数据ID = realDataMain.ID;
                        realDataDetail.污染物编码ID = factorCode.ID;
                        realDataDetail.RTD = Parser.GetFloatByKey(factor + "-Rtd", content) ?? 0;
                        realDataMain.RealDataDetails.Add(realDataDetail);
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }

        public static void Process2051(string content)
        {
            try
            {
                Log.Debug(content);
                var mn = Parser.GetStringByKey("MN", content);
                if (String.IsNullOrWhiteSpace(mn)) return;
                var mapping = CommonInformation.GetTableMappingByPkscMn(mn);
                if (mapping == null) return;
                using (var ctx = new PlatformContext())
                {
                    var dataMain = new MinuteDataMain();
                    dataMain.企业基本信息ID = mapping.enterprise_id;
                    dataMain.设备信息ID = mapping.port_id;
                    dataMain.污染物类别ID = 2;

                    float? masterFactor = null;

                    foreach (var key in MasterFactors.Factors)
                    {
                        masterFactor = Parser.GetFloatByKey(key + "-Cou", content);
                        if (masterFactor != null)
                        {
                            break;
                        }
                    }

                    dataMain.时段量 = masterFactor??0;
                    dataMain.监测时间 = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                    ctx.MinuteDataMains.Add(dataMain);
                    ctx.SaveChanges();
                    var factors = Parser.GetPollutantCodes(content);
                    foreach (var key in MasterFactors.Factors)
                    {
                        factors.Remove(key);
                    }

                    foreach (var factor in factors)
                    {
                        var factorCode = CommonInformation.GetMonitorFactorCodes(factor);
                        if (factorCode == null) continue;
                        var dataDetail = new MinuteDataDetail();
                        dataDetail.主分钟数据ID = dataMain.ID;
                        dataDetail.污染物编码ID = factorCode.ID;
                        dataDetail.Cou = Parser.GetFloatByKey(factor + "-Cou", content) ?? 0;
                        dataDetail.Avg = Parser.GetFloatByKey(factor + "-Avg", content) ?? 0;
                        dataDetail.Max = Parser.GetFloatByKey(factor + "-Max", content) ?? 0;
                        dataDetail.Min = Parser.GetFloatByKey(factor + "-Min", content) ?? 0;
                        dataMain.MinuteDataDetails.Add(dataDetail);
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }

        public static void Process2061(string content)
        {
            try
            {
                Log.Debug(content);
                var mn = Parser.GetStringByKey("MN", content);
                if (String.IsNullOrWhiteSpace(mn)) return;
                var mapping = CommonInformation.GetTableMappingByPkscMn(mn);
                if (mapping == null) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (datatime == null) return;
                var begin = datatime.Value;
                begin = new DateTime(begin.Year, begin.Month, begin.Day, begin.Hour, 0, 0);
                var end = begin.AddHours(1);
                using (var ctx = new PlatformContext())
                {
                    var dataMain = ctx.HourDataMains.Where(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id).FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                    if (dataMain == null)
                    {
                        dataMain= new HourDataMain();
                        ctx.HourDataMains.Add(dataMain);
                    }
                    dataMain.企业基本信息ID = mapping.enterprise_id;
                    dataMain.设备信息ID = mapping.port_id;
                    dataMain.污染物类别ID = 2;

                    float? masterFactor = null;

                    foreach (var key in MasterFactors.Factors)
                    {
                        masterFactor = Parser.GetFloatByKey(key + "-Cou", content);
                        if (masterFactor != null)
                        {
                            break;
                        }
                    }

                    dataMain.时段量 = masterFactor??0;
                    dataMain.监测时间 = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                    
                    ctx.SaveChanges();
                    var factors = Parser.GetPollutantCodes(content);
                    foreach (var key in MasterFactors.Factors)
                    {
                        factors.Remove(key);
                    }

                    foreach (var factor in factors)
                    {
                        var factorCode = CommonInformation.GetMonitorFactorCodes(factor);
                        if (factorCode == null) continue;
                        var dataDetail = dataMain.HourDataDetails.FirstOrDefault(t => t.污染物编码ID == factorCode.ID);
                        if (dataDetail == null)
                        {
                            dataDetail= new HourDataDetail();
                            dataMain.HourDataDetails.Add(dataDetail);
                        }
                        dataDetail.主小时数据ID = dataMain.ID;
                        dataDetail.污染物编码ID = factorCode.ID;
                        dataDetail.Cou = Parser.GetFloatByKey(factor + "-Cou", content) ?? 0;
                        dataDetail.Avg = Parser.GetFloatByKey(factor + "-Avg", content) ?? 0;
                        dataDetail.Max = Parser.GetFloatByKey(factor + "-Max", content) ?? 0;
                        dataDetail.Min = Parser.GetFloatByKey(factor + "-Min", content) ?? 0;
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }

        public static void Process2031(string content)
        {
            try
            {
                Log.Debug(content);
                var mn = Parser.GetStringByKey("MN", content);
                if (String.IsNullOrWhiteSpace(mn)) return;
                var mapping = CommonInformation.GetTableMappingByPkscMn(mn);
                if (mapping == null) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (datatime == null) return;
                var begin = datatime.Value;
                begin = new DateTime(begin.Year, begin.Month, begin.Day, 0, 0, 0);
                var end = begin.AddDays(1);
                using (var ctx = new PlatformContext())
                {
                    var dataMain = ctx.DayDataMains.Where(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id).FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end) ;
                    if (dataMain == null)
                    {
                        dataMain = new DayDataMain();
                        ctx.DayDataMains.Add(dataMain);
                    }
                    dataMain.企业基本信息ID = mapping.enterprise_id;
                    dataMain.设备信息ID = mapping.port_id;
                    dataMain.污染物类别ID = 2;

                    float? masterFactor = null;

                    foreach (var key in MasterFactors.Factors)
                    {
                        masterFactor = Parser.GetFloatByKey(key + "-Cou", content);
                        if (masterFactor != null)
                        {
                            break;
                        }
                    }

                    dataMain.时段量 = masterFactor??0;
                    dataMain.监测时间 = Parser.GetDateTimeByDataTimeThenQnThenNow(content);

                    ctx.SaveChanges();
                    var factors = Parser.GetPollutantCodes(content);
                    foreach (var key in MasterFactors.Factors)
                    {
                        factors.Remove(key);
                    }

                    foreach (var factor in factors)
                    {
                        var factorCode = CommonInformation.GetMonitorFactorCodes(factor);
                        if (factorCode == null) continue;
                        var dataDetail = dataMain.DayDataDetails.FirstOrDefault(t => t.污染物编码ID == factorCode.ID) ;
                        if (dataDetail == null)
                        {
                            dataDetail = new DayDataDetail();
                            dataMain.DayDataDetails.Add(dataDetail);
                        }
                        dataDetail.主日数据ID = dataMain.ID;
                        dataDetail.污染物编码ID = factorCode.ID;
                        dataDetail.Cou = Parser.GetFloatByKey(factor + "-Cou", content) ?? 0;
                        dataDetail.Avg = Parser.GetFloatByKey(factor + "-Avg", content) ?? 0;
                        dataDetail.Max = Parser.GetFloatByKey(factor + "-Max", content) ?? 0;
                        dataDetail.Min = Parser.GetFloatByKey(factor + "-Min", content) ?? 0;
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }

        public static void Process2031ForMonth(string content)
        {
            try
            {
                Log.Debug(content);
                var mn = Parser.GetStringByKey("MN", content);
                if (String.IsNullOrWhiteSpace(mn)) return;
                var mapping = CommonInformation.GetTableMappingByPkscMn(mn);
                if (mapping == null) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (datatime == null) return;
                var begin = datatime.Value;
                begin = new DateTime(begin.Year, begin.Month, 1, 0, 0, 0);
                var end = begin.AddMonths(1);
                using (var ctx = new PlatformContext())
                {
                    var dataMain = ctx.MonthDataMains.Where(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id).FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end)  ;
                    if (dataMain == null)
                    {
                        dataMain=new MonthDataMain();
                        ctx.MonthDataMains.Add(dataMain);
                    }
                    dataMain.企业基本信息ID = mapping.enterprise_id;
                    dataMain.设备信息ID = mapping.port_id;
                    dataMain.污染物类别ID = 2;
                    dataMain.监测时间 = datatime.Value;
                    dataMain.时段量 = ctx.DayDataMains.Where(t => t.监测时间 >= begin && t.监测时间 < end).Where(t => t.时段量 != null).Sum(t => t.时段量);

                    ctx.SaveChanges();
                    var ids = ctx.DayDataMains.Where(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id).Where(t => t.监测时间 >= begin && t.监测时间 < end).Select(t => t.ID);
                    var factors = Parser.GetPollutantCodes(content);
                    foreach (var key in MasterFactors.Factors)
                    {
                        factors.Remove(key);
                    }

                    foreach (var factor in factors)
                    {
                        var factorCode = CommonInformation.GetMonitorFactorCodes(factor);
                        if (factorCode == null) continue;
                        var dataDetail = dataMain.MonthDataDetails.FirstOrDefault(t => t.污染物编码ID == factorCode.ID) ;
                        if (dataDetail == null)
                        {
                            dataDetail = new MonthDataDetail();
                            dataMain.MonthDataDetails.Add(dataDetail);
                        }
                        dataDetail.主月数据ID = dataMain.ID;
                        dataDetail.污染物编码ID = factorCode.ID;
                        dataDetail.Cou = ctx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID)).Where(t => t.污染物编码ID == factorCode.ID).Sum(t => t.Cou);
                        dataDetail.Avg = ctx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID)).Where(t => t.污染物编码ID == factorCode.ID).Average(t => t.Avg);

                        dataDetail.Max = ctx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID)).Where(t => t.污染物编码ID == factorCode.ID).Max(t => t.Max);
                        dataDetail.Min = ctx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID)).Where(t => t.污染物编码ID == factorCode.ID).Min(t => t.Min);
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }

        public static void Process2031ForYear(string content)
        {
            try
            {
                Log.Debug(content);
                var mn = Parser.GetStringByKey("MN", content);
                if (String.IsNullOrWhiteSpace(mn)) return;
                var mapping = CommonInformation.GetTableMappingByPkscMn(mn);
                if (mapping == null) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (datatime == null) return;
                var begin = datatime.Value;
                begin = new DateTime(begin.Year, 1, 1, 0, 0, 0);
                var end = begin.AddYears(1);
                using (var ctx = new PlatformContext())
                {
                    var dataMain = ctx.YearDataMains.Where(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id).FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end)  ;
                    if (dataMain == null)
                    {
                        dataMain = new YearDataMain();
                        ctx.YearDataMains.Add(dataMain);
                    }
                    dataMain.企业基本信息ID = mapping.enterprise_id;
                    dataMain.设备信息ID = mapping.port_id;
                    dataMain.污染物类别ID = 2;
                    dataMain.监测时间 = datatime.Value;
                    dataMain.时段量 = ctx.DayDataMains.Where(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id).Where(t => t.监测时间 >= begin && t.监测时间 < end).Where(t => t.时段量 != null).Sum(t => t.时段量);

                    ctx.SaveChanges();
                    var ids = ctx.DayDataMains.Where(t => t.监测时间 >= begin && t.监测时间 < end).Select(t => t.ID);
                    var factors = Parser.GetPollutantCodes(content);
                    foreach (var key in MasterFactors.Factors)
                    {
                        factors.Remove(key);
                    }

                    foreach (var factor in factors)
                    {
                        var factorCode = CommonInformation.GetMonitorFactorCodes(factor);
                        if (factorCode == null) continue;
                        var dataDetail = dataMain.YearDataDetails.FirstOrDefault(t => t.污染物编码ID == factorCode.ID)  ;
                        if (dataDetail == null)
                        {
                            dataDetail = new YearDataDetail();
                            dataMain.YearDataDetails.Add(dataDetail);
                        }
                        dataDetail.主年数据ID = dataMain.ID;
                        dataDetail.污染物编码ID = factorCode.ID;
                        dataDetail.Cou = ctx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID)).Where(t => t.污染物编码ID == factorCode.ID).Sum(t => t.Cou);
                        dataDetail.Avg = ctx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID)).Where(t => t.污染物编码ID == factorCode.ID).Average(t => t.Avg);

                        dataDetail.Max = ctx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID)).Where(t => t.污染物编码ID == factorCode.ID).Max(t => t.Max);
                        dataDetail.Min = ctx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID)).Where(t => t.污染物编码ID == factorCode.ID).Min(t => t.Min);
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }

    }
}
